$(() => {
    // 登录按钮监听事件
    $('.input_sub').on('click', function () {
        // 获取用户名密码
        let username = $('.input_txt').val()
        let password = $('.input_pass').val()
        // 非空判断
        if (!username.trim() || !password.trim()) {
            $('.modal').modal('show')
            // $('.modal').modal('keyboard')
            $('.modal-body>p').text('请输入用户名密码')
            $('.btn-primary').click(function () { $('.modal').modal('hide') })
            return
        }
        // 获取数据
        $.ajax({
            type: "post",
            url: bigNews.user_login,
            data: { username, password },
            success: function (response) {
                console.log(response);
                // 判断登陆状态
                if (response.code === 200) {
                    window.localStorage.setItem('bignews_token', response.token)
                    $('.modal').modal('show')
                    $('.modal-body>p').text(response.msg)
                    $('.btn-primary').click(function () {
                        $('.modal').modal('hide')
                        // 点击模态框的确定才触发转跳
                        window.location.href = './index.html'
                    })
                } else {
                    $('.modal').modal('show')
                    $('.modal-body>p').text(response.msg)
                    $('.btn-primary').click(function () { $('.modal').modal('hide') })
                }
            }
        });
    })

    $(document).on('keydown', function (e) {
        if (e.keyCode == 13) {
            // alert('你按下了Enter');
            $('.input_sub').click()
        }
    });
})